package com.wl.cloud.datax.common.utils;

import com.wl.cloud.core.exception.CustomException;
import com.wl.cloud.datax.common.enums.DBUtilErrorCode;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author: wanglin
 * @date: 2023-04-17 周一
 * @Version: 1.0
 * @Description:
 */
public enum DataBaseType {
    MySql("mysql", "com.mysql.cj.jdbc.Driver"),
    Oracle("oracle", "oracle.jdbc.OracleDriver"),
    PostgreSQL("postgresql", "org.postgresql.Driver"),
    SQLServer("sqlserver", "com.microsoft.sqlserver.jdbc.SQLServerDriver"),
    Hive("hive", "org.apache.hive.jdbc.HiveDriver"),
    IMPALA("impala", "com.cloudera.impala.jdbc.Driver");

    private String key;
    private String value;

    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }

    private DataBaseType(String key, String value) {
        this.key = key;
        this.value = value;
    }

    public static DataBaseType getEnumByKey(String getTypeName) {
        if (getTypeName == null) {
            return null;
        }
        for (DataBaseType e : DataBaseType.values()) {
            if (e.getKey().equals(getTypeName)) {
                return e;
            }
        }
        return null;
    }

    public static String getValueByKey(String key) {
        if (key == null) {
            return null;
        }
        for (DataBaseType e : DataBaseType.values()) {
            if (e.getKey().equals(key)) {
                return e.getValue();
            }
        }
        return null;
    }

    public static String getKeyByValue(String value) {
        if (StringUtils.isNotEmpty(value)) {
            for (DataBaseType e : DataBaseType.values()) {
                if (e.getValue().equals(value)) {
                    return e.getKey();
                }
            }
        }
        return null;
    }

    public static List<String> getKeys() {
        List<String> keyList = new ArrayList();
        for (DataBaseType e : DataBaseType.values()) {
            keyList.add(e.getKey());
        }
        return keyList;
    }


    public String appendJDBCSuffixForReader(String jdbc, String dbName) {
        if (!isEndWithNumber(jdbc)) {
            return jdbc;
        }
        switch (this) {
            case MySql:
                jdbc += "/" + dbName + "?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&allowMultiQueries=true&serverTimezone=GMT%2B8&useCursorFetch=true";
                break;
            case SQLServer:
                jdbc += ";databaseName=" + dbName;
                break;
            case Hive:
            case Oracle:
            case PostgreSQL:
                jdbc += "/" + dbName;
                break;
            default:
                throw CustomException.asCustomException(DBUtilErrorCode.UNSUPPORTED_TYPE, "unsupported database type.");
        }
        return jdbc;
    }

    private static Pattern mysqlPattern = Pattern.compile("jdbc:mysql://(.+):\\d+/.+");
    private static Pattern oraclePattern = Pattern.compile("jdbc:oracle:thin:@(.+):\\d+:.+");
    private static Pattern numberPattern = Pattern.compile("[0-9]*");

    /**
     * 注意：目前只实现了从 mysql/oracle 中识别出ip 信息.未识别到则返回 null.
     */
    public static String parseIpFromJdbcUrl(String jdbcUrl) {
        Matcher mysql = mysqlPattern.matcher(jdbcUrl);
        if (mysql.matches()) {
            return mysql.group(1);
        }
        Matcher oracle = oraclePattern.matcher(jdbcUrl);
        if (oracle.matches()) {
            return oracle.group(1);
        }
        return null;
    }

    public static Boolean isEndWithNumber(String str) {
        try {
            Matcher isNum = numberPattern.matcher(str.charAt(str.length() - 1) + "");
            if (!isNum.matches()) {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
